import { JKManager } from "../JKManager"
import { NativeNotificationConfig, NotificationConfig, NotificationConfigs, NotifyPeriod } from "./NativeNotificationConfig"

/**
 * 手机本地推送通知工具单例
 */
export class NativeNotification {
    init() {
        cc.log(`${cc.js.getClassName(this)} init`)

        let notifies: { [key: string]: string }[] = [],
            configs: NotificationConfigs = NativeNotificationConfig
        for (let index = 1; index <= 100; index++) {
            let title = JKManager.Language.getText(`push${index}`),
                msg = JKManager.Language.getText(`pushmessage${index}`)
            if (msg) {
                notifies.push({
                    title: title,
                    msg: msg,
                })
            } else {
                break
            }
        }
        if (configs.time instanceof Array) {
            configs.time.forEach((notifyPeriod: NotifyPeriod) => {
                let id = notifyPeriod.id,
                    msgId = notifyPeriod.msgId,
                    weekDay = notifyPeriod.weekDay,
                    times = notifyPeriod.times,
                    period = (Number(notifyPeriod.period.split(`:`)[0]) * 60 + Number(notifyPeriod.period.split(`:`)[1])) * 60 * 1000,
                    title = notifies[msgId - 1].title,
                    msg = notifies[msgId - 1].msg,
                    date = new Date()
                times.forEach((clock: string) => {
                    let hour = Number(clock.split(`:`)[0]),
                        min = Number(clock.split(`:`)[1]),
                        offDay = weekDay - date.getDay()
                    if (offDay < 0) {
                        offDay = offDay + 7
                    }
                    if (offDay === 0) {
                        // 当天不提示
                        date.setDate(date.getDate() + 7)
                    } else {
                        date.setDate(date.getDate() + offDay)
                    }
                    date.setHours(hour, min, 0)
                    let time = date.getTime()
                    cc.log(`${cc.js.getClassName(this)} init offTime = ${JKManager.Utils.formatTime(time - Date.now(), ``, ``, `d`, `h`, `m`, `s`)}`)

                    // 测试
                    // time = Date.now()
                    // period = 15 * 60 * 1000

                    this.nativeNotify(`${id}`, title, msg, `${time}`, `${period}`)
                })
            })
        }
    }

    //==============================================================================================================================
    //========================================================= 内部方法 ============================================================
    //==============================================================================================================================

    /**
     * 
     * @param id 记录，避免通知重复覆盖
     */
    private _record(id: string, title: string, msg: string, time: string, period: string) {
        console.log(`${cc.js.getClassName(this)} _record id = ${id} title = ${title} msg = ${msg} time = ${time} period = ${period}`)

        !!cc.sys.localStorage.getItem(`notify_${id}`) && cc.sys.localStorage.setItem(`notify_${id}`, {
            id: id,
            title: title,
            msg: msg,
            time: time,
            period: period,
        })
    }

    //==============================================================================================================================
    //========================================================= 外部方法 ============================================================
    //==============================================================================================================================

    /**
     * 推送通知
     * @param id id
     * @param title 标题
     * @param msg 内容
     * @param time 推送时间，毫秒
     * @param period 周期，毫秒，默认一天
     */
    nativeNotify(id: string, title: string, msg: string, time: string, period: string = `${24 * 60 * 60 * 1000}`) {
        console.log(`${cc.js.getClassName(this)} nativeNotify id = ${id} title = ${title} msg = ${msg} time = ${time} period = ${period}`)

        this._record(id, title, msg, time, period)
        if (!CC_PREVIEW) {
            if (cc.sys.isNative && !cc.sys.isBrowser) {
                if (cc.sys.os === cc.sys.OS_ANDROID) {
                    jsb.reflection.callStaticMethod(`jkCore/nativeNotification/NativeNotification`, `notify`, `(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V`, id, title, msg, time, period)
                } else if (cc.sys.os === cc.sys.OS_IOS) {
                    jsb.reflection.callStaticMethod(`NativeNotification`, `notifyWithId:title:msg:time:period:`, id, title, msg, time, period)
                }
            }
        }
    }
}